package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.Links;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:16
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class LinksDao extends BaseDao<Links> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<Links> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<Links> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<Links> list = run.query(sql, new ResultSetHandler<List<Links>>() {
            @Override
            public List<Links> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    Links links = new Links();

                    links.setId(rs.getInt("id"));
                    links.setCreated(rs.getTimestamp("created"));
                    links.setName(rs.getString("name"));
//                    links.setUpdated(rs.getTimestamp("updated"));
                    links.setUrl(rs.getString("url"));

                    list.add(links);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  Links 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public Links selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, created, name, updated, url from mto_links where id = ?";
        Links links = run.query(sql,new ResultSetHandler<Links>() {
            @Override
            public Links handle(ResultSet rs) throws SQLException {
                Links links = null;
                if(rs.next()) {
                    links = new Links();

                        links.setId(rs.getInt("id"));
                        links.setCreated(rs.getTimestamp("created"));
                        links.setName(rs.getString("name"));
//                        links.setUpdated(rs.getTimestamp("updated"));
                        links.setUrl(rs.getString("url"));
                }
                return  links;
            }
        },params);
        return links;
    }

    /**
    *
    * @param links 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(Links links) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_links (  ");
        sql.append("  created, name, updated, url ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?,   ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            links.getCreated(),  
            links.getName(),  
//            links.getUpdated(),
            links.getUrl()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  links
    * @return
    * @throws SQLException
    */
    @Override
    public int update(Links links) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_links           ");
        sql.append(" SET                    ");
            
            sql.append("   created  = ?,  ");
            sql.append("   name  = ?,  ");
//            sql.append("   updated  = ?,  ");
            sql.append("   url  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                links.getCreated(), 
                links.getName(), 
//                links.getUpdated(),
                links.getUrl(), 
            links.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_links where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
